#!/usr/bin/env python3
"""
Element UI 按钮样式演示
展示改进后的Element UI风格按钮样式
"""

import sys
import os

# 添加项目根目录到Python路径
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

try:
    from PySide6.QtWidgets import *
    from PySide6.QtCore import *
    from PySide6.QtGui import *
except ImportError:
    try:
        from PyQt6.QtWidgets import *
        from PyQt6.QtCore import *
        from PyQt6.QtGui import *
    except ImportError:
        try:
            from PySide2.QtWidgets import *
            from PySide2.QtCore import *
            from PySide2.QtGui import *
        except ImportError:
            from PyQt5.QtWidgets import *
            from PyQt5.QtCore import *
            from PyQt5.QtGui import *

from element_ui.styles import (
    theme_manager,
    style_engine,
    Theme,
    create_button_style
)


class ElementButton(QPushButton):
    """Element UI 风格按钮组件"""
    
    def __init__(self, text="", button_type="default", size="default", plain=False, parent=None):
        super().__init__(text, parent)
        self.button_type = button_type
        self.size = size
        self.plain = plain
        self._setup_button()
    
    def _setup_button(self):
        """设置按钮样式"""
        # 获取按钮样式配置
        style_config = create_button_style(self.button_type, self.size, self.plain)
        
        # 应用样式到按钮
        self._apply_style(style_config)
        
        # 设置鼠标悬停效果
        self.setMouseTracking(True)
        
    def _apply_style(self, style_config):
        """应用样式配置到QPushButton"""
        # 基础样式
        base_style = f"""
            QPushButton {{
                background-color: {style_config.get('background-color', '#ffffff')};
                color: {style_config.get('color', '#606266')};
                border: {style_config.get('border-width', '1px')} {style_config.get('border-style', 'solid')} {style_config.get('border-color', '#dcdfe6')};
                border-radius: {style_config.get('border-radius', '4px')};
                padding: {style_config.get('padding', '8px 15px')};
                font-size: {style_config.get('font-size', '14px')};
                font-weight: {style_config.get('font-weight', '500')};
                font-family: {style_config.get('font-family', 'Microsoft YaHei, Arial, sans-serif')};
                text-align: center;
                outline: none;
            }}
        """
        
        # 悬停样式
        hover_config = style_config.get('hover', {})
        if hover_config:
            hover_style = f"""
                QPushButton:hover {{
                    background-color: {hover_config.get('background-color', style_config.get('background-color', '#ffffff'))};
                    color: {hover_config.get('color', style_config.get('color', '#606266'))};
                    border-color: {hover_config.get('border-color', style_config.get('border-color', '#dcdfe6'))};
                }}
            """
            base_style += hover_style
        
        # 按下样式
        active_config = style_config.get('active', {})
        if active_config:
            pressed_style = f"""
                QPushButton:pressed {{
                    background-color: {active_config.get('background-color', style_config.get('background-color', '#ffffff'))};
                    color: {active_config.get('color', style_config.get('color', '#606266'))};
                    border-color: {active_config.get('border-color', style_config.get('border-color', '#dcdfe6'))};
                }}
            """
            base_style += pressed_style
        
        # 禁用样式
        disabled_config = style_config.get('disabled', {})
        if disabled_config:
            disabled_style = f"""
                QPushButton:disabled {{
                    background-color: {disabled_config.get('background-color', '#f5f7fa')};
                    color: {disabled_config.get('color', '#c0c4cc')};
                    border-color: {disabled_config.get('border-color', '#ebeef5')};
                    cursor: not-allowed;
                }}
            """
            base_style += disabled_style
        
        self.setStyleSheet(base_style)
        
        # 设置按钮尺寸
        height = int(style_config.get('height', '32px').replace('px', ''))
        self.setFixedHeight(height)
        self.setMinimumWidth(64)


class ButtonShowcase(QWidget):
    """按钮展示窗口"""
    
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Element UI 按钮样式演示")
        self.setGeometry(100, 100, 1200, 800)
        self.setup_ui()
    
    def setup_ui(self):
        """设置UI"""
        layout = QVBoxLayout(self)
        layout.setSpacing(30)
        layout.setContentsMargins(30, 30, 30, 30)
        
        # 标题
        title = QLabel("Element UI 风格按钮样式演示")
        title.setStyleSheet("""
            QLabel {
                font-size: 24px;
                font-weight: bold;
                color: #303133;
                margin-bottom: 20px;
            }
        """)
        layout.addWidget(title)
        
        # 主题切换区域
        theme_group = self.create_theme_group()
        layout.addWidget(theme_group)
        
        # 基础按钮区域
        basic_group = self.create_basic_buttons_group()
        layout.addWidget(basic_group)
        
        # 朴素按钮区域
        plain_group = self.create_plain_buttons_group()
        layout.addWidget(plain_group)
        
        # 不同尺寸按钮区域
        size_group = self.create_size_buttons_group()
        layout.addWidget(size_group)
        
        # 按钮状态演示区域
        state_group = self.create_button_states_group()
        layout.addWidget(state_group)
        
        layout.addStretch()
    
    def create_theme_group(self):
        """创建主题切换组"""
        group = QGroupBox("主题切换")
        group.setStyleSheet("""
            QGroupBox {
                font-weight: bold;
                font-size: 16px;
                color: #303133;
                border: 2px solid #dcdfe6;
                border-radius: 5px;
                margin-top: 10px;
                padding-top: 10px;
            }
            QGroupBox::title {
                subcontrol-origin: margin;
                left: 10px;
                padding: 0 5px 0 5px;
            }
        """)
        
        layout = QHBoxLayout(group)
        
        # 默认主题按钮
        default_btn = ElementButton("默认主题", "primary", "default")
        default_btn.clicked.connect(lambda: self.switch_theme("element-default"))
        layout.addWidget(default_btn)
        
        # 暗色主题按钮
        dark_btn = ElementButton("暗色主题", "info", "default")
        dark_btn.clicked.connect(lambda: self.switch_theme("element-dark"))
        layout.addWidget(dark_btn)
        
        # 当前主题显示
        self.current_theme_label = QLabel(f"当前主题: {theme_manager.get_current_theme()}")
        self.current_theme_label.setStyleSheet("color: #909399; font-size: 14px;")
        layout.addWidget(self.current_theme_label)
        
        layout.addStretch()
        
        return group
    
    def create_basic_buttons_group(self):
        """创建基础按钮组"""
        group = QGroupBox("基础按钮 (不同类型)")
        group.setStyleSheet("""
            QGroupBox {
                font-weight: bold;
                font-size: 16px;
                color: #303133;
                border: 2px solid #dcdfe6;
                border-radius: 5px;
                margin-top: 10px;
                padding-top: 10px;
            }
            QGroupBox::title {
                subcontrol-origin: margin;
                left: 10px;
                padding: 0 5px 0 5px;
            }
        """)
        
        layout = QHBoxLayout(group)
        layout.setSpacing(15)
        
        button_types = [
            ("默认", "default"),
            ("主要", "primary"),
            ("成功", "success"), 
            ("信息", "info"),
            ("警告", "warning"),
            ("危险", "danger")
        ]
        
        for text, btn_type in button_types:
            btn = ElementButton(text, btn_type, "default")
            btn.clicked.connect(lambda checked, t=text: self.on_button_click(f"{t}按钮被点击"))
            layout.addWidget(btn)
        
        layout.addStretch()
        
        return group
    
    def create_plain_buttons_group(self):
        """创建朴素按钮组"""
        group = QGroupBox("朴素按钮 (Plain样式)")
        group.setStyleSheet("""
            QGroupBox {
                font-weight: bold;
                font-size: 16px;
                color: #303133;
                border: 2px solid #dcdfe6;
                border-radius: 5px;
                margin-top: 10px;
                padding-top: 10px;
            }
            QGroupBox::title {
                subcontrol-origin: margin;
                left: 10px;
                padding: 0 5px 0 5px;
            }
        """)
        
        layout = QHBoxLayout(group)
        layout.setSpacing(15)
        
        button_types = [
            ("默认", "default"),
            ("主要", "primary"), 
            ("成功", "success"),
            ("信息", "info"),
            ("警告", "warning"),
            ("危险", "danger")
        ]
        
        for text, btn_type in button_types:
            btn = ElementButton(text, btn_type, "default", plain=True)
            btn.clicked.connect(lambda checked, t=text: self.on_button_click(f"朴素{t}按钮被点击"))
            layout.addWidget(btn)
        
        layout.addStretch()
        
        return group
    
    def create_size_buttons_group(self):
        """创建不同尺寸按钮组"""
        group = QGroupBox("按钮尺寸")
        group.setStyleSheet("""
            QGroupBox {
                font-weight: bold;
                font-size: 16px;
                color: #303133;
                border: 2px solid #dcdfe6;
                border-radius: 5px;
                margin-top: 10px;
                padding-top: 10px;
            }
            QGroupBox::title {
                subcontrol-origin: margin;
                left: 10px;
                padding: 0 5px 0 5px;
            }
        """)
        
        layout = QHBoxLayout(group)
        layout.setSpacing(15)
        
        sizes = [
            ("大号", "large"),
            ("默认", "default"),
            ("小号", "small"),
            ("迷你", "mini")
        ]
        
        for text, size in sizes:
            btn = ElementButton(f"{text}按钮", "primary", size)
            btn.clicked.connect(lambda checked, t=text: self.on_button_click(f"{t}按钮被点击"))
            layout.addWidget(btn)
        
        layout.addStretch()
        
        return group
    
    def create_button_states_group(self):
        """创建按钮状态演示组"""
        group = QGroupBox("按钮状态")
        group.setStyleSheet("""
            QGroupBox {
                font-weight: bold;
                font-size: 16px;
                color: #303133;
                border: 2px solid #dcdfe6;
                border-radius: 5px;
                margin-top: 10px;
                padding-top: 10px;
            }
            QGroupBox::title {
                subcontrol-origin: margin;
                left: 10px;
                padding: 0 5px 0 5px;
            }
        """)
        
        layout = QHBoxLayout(group)
        layout.setSpacing(15)
        
        # 正常按钮
        normal_btn = ElementButton("正常状态", "primary", "default")
        normal_btn.clicked.connect(lambda: self.on_button_click("正常状态按钮被点击"))
        layout.addWidget(normal_btn)
        
        # 禁用按钮
        disabled_btn = ElementButton("禁用状态", "primary", "default")
        disabled_btn.setEnabled(False)
        layout.addWidget(disabled_btn)
        
        # 朴素禁用按钮
        plain_disabled_btn = ElementButton("朴素禁用", "success", "default", plain=True)
        plain_disabled_btn.setEnabled(False)
        layout.addWidget(plain_disabled_btn)
        
        layout.addStretch()
        
        return group
    
    def switch_theme(self, theme_name):
        """切换主题"""
        if theme_manager.switch_theme(theme_name):
            self.current_theme_label.setText(f"当前主题: {theme_name}")
            # 重新创建所有按钮以应用新主题
            self.recreate_buttons()
    
    def recreate_buttons(self):
        """重新创建所有按钮以应用新主题"""
        # 这里可以实现重新创建按钮的逻辑
        # 为了简化，我们显示一个消息
        QMessageBox.information(self, "主题切换", "主题切换成功！\n重启应用以查看所有组件的新主题效果。")
    
    def on_button_click(self, message):
        """按钮点击事件处理"""
        print(f"[按钮点击] {message}")
        
        # 显示点击反馈
        self.statusBar = getattr(self, 'statusBar', None)
        if not self.statusBar:
            self.statusBar = QLabel(message)
            self.statusBar.setStyleSheet("""
                QLabel {
                    color: #67c23a;
                    font-size: 14px;
                    padding: 5px;
                    background-color: #f0f9ff;
                    border: 1px solid #c2e7b0;
                    border-radius: 4px;
                }
            """)
            # 添加到主布局的底部
            self.layout().addWidget(self.statusBar)
        
        self.statusBar.setText(f"✓ {message}")
        
        # 3秒后清空消息
        QTimer.singleShot(3000, lambda: self.statusBar.setText(""))


def main():
    """主函数"""
    app = QApplication(sys.argv)
    
    # 设置应用样式
    app.setStyle('Fusion')
    
    # 设置全局字体
    font = QFont("Microsoft YaHei", 10)
    app.setFont(font)
    
    # 设置应用图标和名称
    app.setApplicationName("Element UI Button Showcase")
    app.setApplicationVersion("1.0")
    
    # 创建主窗口
    window = ButtonShowcase()
    window.show()
    
    print("Element UI 按钮样式演示应用启动成功！")
    print("功能说明：")
    print("- 展示Element UI风格的按钮样式")
    print("- 支持不同类型、尺寸和状态的按钮")
    print("- 支持主题切换")
    print("- 所有按钮都可以点击测试")
    
    return app.exec()


if __name__ == "__main__":
    sys.exit(main())
